package com.start.platform.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.start.common.api.Result;
import com.start.common.dataRule.DataQueryConstructor;
import com.start.core.annotations.RegisterMenu;
import com.start.core.annotations.RegisterPermission;
import com.start.core.annotations.StartLog;
import com.start.platform.core.StartPlatformManage;
import com.start.platform.core.entity.StartPlatformConfigEntity;
import com.start.platform.core.service.StartPlatformConfigService;
import com.start.security.annotations.DataPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/start/platform/config")
@RegisterMenu(value = "平台配置", parentId = "fa082311783863635318d8e0ed424554", systemCode = "start:platform:config:", component = "views/start/platform/config/index", path = "/platform/config/index")
public class StartPlatformConfigController {
    private static final String LOG_TITLE = "平台配置";

    @Autowired
    private StartPlatformConfigService platformConfigService;


    /**
     * 列表
     */
    @GetMapping("/page")
    @DataPermission("views/start/platform/config/index")
    public Result<?> list(Page<StartPlatformConfigEntity> page, StartPlatformConfigEntity entity) {
        QueryWrapper<StartPlatformConfigEntity> queryWrapper = DataQueryConstructor.createEntity(entity);
        return Result.ok(platformConfigService.page(page, queryWrapper));
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @StartLog(value = LOG_TITLE, action = "查询")
    @PreAuthorize("@pas.hasPermission('start:platform:config:info')")
    @RegisterPermission(value = "start:platform:config:info", name = "查看")
    public Result<?> info(@PathVariable("id") String id) {
        return Result.ok(platformConfigService.getById(id));
    }

    /**
     * 保存
     */
    @PostMapping("/add")
    @StartLog(value = LOG_TITLE, action = "添加")
    @PreAuthorize("@pas.hasPermission('start:platform:config:add')")
    @RegisterPermission(value = "start:platform:config:add", name = "添加")
    public Result<?> save(@RequestBody StartPlatformConfigEntity entity) {
        platformConfigService.save(entity);
        StartPlatformManage.getInstance().add(entity.getPlatformType(), entity);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/updateById")
    @StartLog(value = LOG_TITLE, action = "修改")
    @PreAuthorize("@pas.hasPermission('start:platform:config:edit')")
    @RegisterPermission(value = "start:platform:config:edit", name = "修改")
    public Result<?> update(@RequestBody StartPlatformConfigEntity entity) {
        StartPlatformConfigEntity oldEntity = platformConfigService.getById(entity.getId());
        if (StartPlatformManage.getInstance().hasPlatform(oldEntity.getPlatformType(), entity.getAppId())) {
            StartPlatformManage.getInstance().remove(entity.getPlatformType(), entity.getAppId());
        }
        platformConfigService.updateById(entity);
        if (entity.getIsEnable() == 1) {
            StartPlatformManage.getInstance().add(entity.getPlatformType(), entity);
        }
        return Result.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/deleteById/{id}")
    @StartLog(value = LOG_TITLE, action = "删除")
    @PreAuthorize("@pas.hasPermission('start:platform:config:delete')")
    @RegisterPermission(value = "start:platform:config:delete", name = "删除")
    public Result<?> delete(@PathVariable("id") String id) {
        final StartPlatformConfigEntity entity = platformConfigService.getById(id);
        if (entity == null) return Result.ok();
        platformConfigService.removeById(id);
        StartPlatformManage.getInstance().remove(entity.getPlatformType(), entity.getAppId());
        return Result.ok();
    }

    /**
     * 是否启用
     */
    @GetMapping("/changeEnable/{id}")
    @StartLog(value = LOG_TITLE, action = "启用或禁用第三方平台")
    @PreAuthorize("@pas.hasPermission('start:platform:config:changeEnable')")
    @RegisterPermission(value = "start:platform:config:changeEnable", name = "启用或禁用第三方平台")
    public Result<?> changeEnable(@PathVariable("id") String id, Integer state) {
        if (state == null) return Result.fail("平台状态不能为空");
        final StartPlatformConfigEntity entity = platformConfigService.getById(id);
        if (entity == null) return Result.fail("未找到id为" + id + "的第三方平台配置");
        if (state == 1) {
            if (!StartPlatformManage.getInstance().hasPlatform(entity.getPlatformType(), entity.getAppId())) {
                StartPlatformManage.getInstance().add(entity.getPlatformType(), entity);
            }
        } else {
            StartPlatformManage.getInstance().remove(entity.getPlatformType(), entity.getAppId());
        }
        entity.setIsEnable(state);
        platformConfigService.updateById(entity);
        return Result.ok();
    }

    @GetMapping("/getAllConfig")
    @StartLog(value = LOG_TITLE, action = "获取全部平台配置")
    @PreAuthorize("@pas.hasPermission('start:platform:config:platformMigration')")
    @DataPermission("tpp/thirdPartyPlatformConfig/list")
    public Result<?> getAllConfig() {
        final List<StartPlatformConfigEntity> list = platformConfigService.list();
        return Result.ok(list);
    }
    @GetMapping("/getAllPlatformType")
    public Result<?> getAllPlatformType() {
        return Result.ok(StartPlatformManage.getInstance().getAllPlatformType());
    }

//    /**
//     * 删除
//     */
//    @GetMapping("/platformMigration" )
//    @StartLog(value = LOG_TITLE, action = "平台用户迁移" )
//    @PreAuthorize("@pas.hasPermission('start:platform:config:platformMigration')" )
//    @RegisterPermission(value = "start:platform:config:platformMigration", name = "平台用户迁移" )
//    public Result<?> platformMigration(String oldPlatformId, String newPlatformId) {
//        final StartPlatformConfigEntity oldConfigEntity = thirdPartyPlatformConfigService.getById(oldPlatformId);
//        if (oldConfigEntity == null) return Result.fail("未找到迁移的源平台" );
//        final StartPlatformConfigEntity newConfigEntity = thirdPartyPlatformConfigService.getById(newPlatformId);
//        if (newConfigEntity == null) return Result.fail("未找到迁移的新平台" );
//        if (!oldConfigEntity.getPlatformType().equals(newConfigEntity.getPlatformType()))
//            return Result.fail("旧平台类型与新平台类型不同" );
//        if (!transferServiceMap.containsKey(oldConfigEntity.getPlatformType())) return Result.fail("未找到对应平台类型请联系管理员" );
//        final PlatformTransferService platformTransferService = transferServiceMap.get(oldConfigEntity.getPlatformType());
//        final PlatformTransferResult result = platformTransferService.transferUserInfo(oldConfigEntity, newConfigEntity);
//        return Result.ok(result);
//    }

}
